﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Messaging;
using OpenComposite.Base.Collections;

namespace OpenComposite.EII.Designer.Workflow.Forms
{
	public partial class FormMsmqConfig : Form
	{
		public FormMsmqConfig()
		{
			InitializeComponent();
			this.DialogResult = DialogResult.None;
			dgvMessageQueues.AutoGenerateColumns = false;
			foreach ( MessageQueue msmq in MessageQueue.GetPrivateQueuesByMachine(".") ) {
				MsmqItem item = new MsmqItem();
				item.Name = msmq.Label;
				item.IsPublic = false;
				item.Path = msmq.Path;
				_item.Add(item);
			}
			try {
				foreach ( MessageQueue msmq in MessageQueue.GetPublicQueues() ) {
					MsmqItem item = new MsmqItem();
					item.Name = msmq.Label;
					item.IsPublic = true;
					item.Path = msmq.Path;
					_item.Add(item);
				}
			} catch { }
			dgvMessageQueues.DataSource = _item;
			this.DialogResult = DialogResult.None;
		}

		BindingListEx<MsmqItem> _item = new BindingListEx<MsmqItem>();
		private void FormMsmqConfig_Load(object sender, EventArgs e)
		{
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			save();
		}

		private void save()
		{
			this.DialogResult = DialogResult.OK;
			Close();
		}

		private void lbQueues_SelectedIndexChanged(object sender, EventArgs e)
		{
			MsmqItem selected = dgvMessageQueues.SelectedRows[0].DataBoundItem as MsmqItem;
			txtName.Text = selected.Path;
		}

		public string MessageQueuePath
		{
			get { return getSelectedId(); }
			set { setSelectedId(value); }
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			try {
				MessageQueue msmq = new MessageQueue(txtName.Text);
				// CreateTime throws ArgumentOutOfRangeException if message queue was not found
				if ( msmq.CreateTime < DateTime.MaxValue ) {
					foreach ( MsmqItem item in _item ) {
						if ( item.Path == msmq.Path ) {
							MessageBox.Show("Error: A Message Queue with the same path already exists in the list!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
							return;
						}
					}
					MsmqItem newItem = new MsmqItem();
					newItem.Name = msmq.Label;
					newItem.IsPublic = !msmq.Path.Contains("private$");
					newItem.Path = msmq.Path;
					_item.Add(newItem);
				} else {
					MessageBox.Show("Error: Invalid value for CreateTime of Message Queue.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			} catch {
				MessageBox.Show("Error: Message Queue with the specified path could not be found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		class MsmqItem
		{
			public string Name { get; set; }
			public bool IsPublic { get; set; }
			public string Path { get; set; }
			public bool Checked { get; set; }
		}

		private string getSelectedId()
		{
			foreach ( DataGridViewRow row in dgvMessageQueues.Rows ) {
				MsmqItem item = row.DataBoundItem as MsmqItem;
				if ( item.Checked ) return item.Path;
			}
			return null;
		}

		private void setSelectedId(MsmqItem value)
		{
			setSelectedId(value.Path);
		}
		private void setSelectedId(string value)
		{
			foreach ( DataGridViewRow row in dgvMessageQueues.Rows ) {
				MsmqItem item = row.DataBoundItem as MsmqItem;
				item.Checked = item.Path == value;
			}
		}

		private void dgvMessageQueues_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			if ( e.RowIndex < 0 ) return;
			if ( e.ColumnIndex == colChecked.Index ) {
				MsmqItem item = dgvMessageQueues.Rows[e.RowIndex].DataBoundItem as MsmqItem;
				if ( item == null ) return;
				item.Checked = !item.Checked;
				if ( item.Checked )
					setSelectedId(item);
				dgvMessageQueues.InvalidateColumn(colChecked.Index);
			}
		}
	}
}
